package com.tarena.lbs.basic.service.impl;

import com.tarena.lbs.basic.dao.mapper.AreaMapper;
import com.tarena.lbs.basic.pojo.po.AreaPo;
import com.tarena.lbs.basic.pojo.vo.AreaVO;
import com.tarena.lbs.basic.service.AreaService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
public class AreaServiceImpl implements AreaService {

    @Resource
    private AreaMapper areaMapper;

    @Override
    public List<AreaVO> queryProvinces() {
        List<AreaPo> provincePOs = areaMapper.selectProvinces();
        return provincePOs.stream()
                .map((AreaPo po) -> { // 显式声明参数类型 AreaPo
                    AreaVO vo = new AreaVO();
                    vo.setId(po.getId());
                    vo.setParentId(po.getParentId());
                    vo.setName(po.getName());
                    vo.setDepth(po.getDepth());
                    return vo;
                })
                .collect(Collectors.toList());
    }




    @Override
    public List<AreaVO> queryChildren(Long parentId) {
        if (parentId == null) {
            return new ArrayList<>();
        }

        List<AreaPo> childPOs = areaMapper.selectByParentId(parentId);
        List<AreaVO> childVOs = new ArrayList<AreaVO>();
        for(AreaPo po:childPOs){
            AreaVO vo = new AreaVO();
            vo.setId(po.getId());
            vo.setParentId(po.getParentId());
            vo.setName(po.getName());
            vo.setDepth(po.getDepth());
            childVOs.add(vo);
        }
        return childVOs;
        /*childPOs.stream().map(new Function<AreaPo, Object>() {
            @Override
            public Object apply(AreaPo po) {
                AreaVO vo = new AreaVO();
                    vo.setId(po.getId());
                    vo.setParentId(po.getParentId());
                    vo.setName(po.getName());
                    vo.setDepth(po.getDepth());
                    return vo;
            }
        }).collect(Collectors.toList());*/


//        return childPOs.stream()
//                .map((AreaPo po) -> { // 显式声明参数类型 AreaPo
//                    AreaVO vo = new AreaVO();
//                    vo.setId(po.getId());
//                    vo.setParentId(po.getParentId());
//                    vo.setName(po.getName());
//                    vo.setDepth(po.getDepth());
//                    return vo;
//                })
//                .collect(Collectors.toList());
    }




    @Override
    public List<AreaVO> queryAreaTree() {
        List<AreaPo> allPOs = areaMapper.selectAll();
        if (allPOs.isEmpty()) {
            return new ArrayList<>();
        }

        // 显式类型转换的 map 操作
        Map<Long, List<AreaVO>> childMap = allPOs.stream()
                .map((AreaPo po) -> { // 显式声明参数类型 AreaPo
                    AreaVO vo = new AreaVO();
                    vo.setId(po.getId());
                    vo.setParentId(po.getParentId());
                    vo.setName(po.getName());
                    vo.setDepth(po.getDepth());
                    return vo;
                })
                .collect(Collectors.groupingBy(AreaVO::getParentId));

        List<AreaVO> rootAreas = childMap.get(0L);
        if (rootAreas == null) {
            return new ArrayList<>();
        }

        rootAreas.forEach(province -> {
            List<AreaVO> cities = childMap.get(province.getId());
            if (cities != null) {
                province.setChildren(cities);
                cities.forEach(city -> city.setChildren(childMap.get(city.getId())));
            }
        });

        return rootAreas;
    }
}